﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media;
namespace BallData
{
    // 显示比赛场次的逻辑
    class ShowMathItemLogic : IListenMatchItem
    {
        private MainUIControl uicontrol;

        public MainUIControl Uicontrol
        {
            get { return uicontrol; }
            set { uicontrol = value; }
        }
        private Hashtable otherleaguematchs = new Hashtable();

        // 存储其他联盟的比赛场次
        public Hashtable Otherleaguematchs
        {
            get { return otherleaguematchs; }
            set { otherleaguematchs = value; }
        }

        public Hashtable leagueflag = new Hashtable();

       
        void item_Click(object sender, RoutedEventArgs e)
        {
            MenuItem se = sender as MenuItem;

            if (se != null)
            {
                se.Background = new SolidColorBrush(Colors.White);

                foreach (LeagueItem litem in this.Uicontrol.getMatchData().LeagueList.Values)
                {
                    if (litem.cnName == se.Header as String)
                    {
                        leagueflag.Remove(litem.cnName);

                        ObservableCollection<MatchListDataTemple> matchlist = OddsUntil.LeagueViewTemple[litem.lId] as ObservableCollection<MatchListDataTemple>;

                        foreach (TabItem tb in this.Uicontrol.getLeagueTab().Items)
                        {
                            if (tb.Header as String == "其他")
                            {
                                var sortlist = matchlist.OrderBy(p => p.lMatchKoTime);
                                matchlist = new ObservableCollection<MatchListDataTemple>(sortlist);

                                tb.DataContext = litem.lId;
                                ListView listviewother = tb.Content as ListView;
                                listviewother.ItemsSource = matchlist;

                                OddsUntil.LeagueViewTemple[litem.lId] = matchlist;

                                break;
                            }
                        }
                    }


                }

            }

            if (leagueflag.Count == 0)
            {
                foreach (TabItem tb in this.Uicontrol.getLeagueTab().Items)
                {
                    if (tb.Header as String == "其他")
                    {
                        tb.Background = OddsUntil.tabdefaultdefault;
                    }
                }
            }

        }
        // 监听遍历比赛场次结束
        public void ListenEnd()
        {
            Hashtable matchcounts = new Hashtable();

            foreach (DictionaryEntry entry in Otherleaguematchs)
	        {
               List<String> l = entry.Value as List<String>;
	           matchcounts.Add(entry.Key, l.Count);
	        }

            ArrayList sort = new ArrayList(matchcounts.Values);

            // 排序下联盟中比赛最多的
            sort.Sort();
            int showotherleaguecount = 0;

           

            
            ArrayList showtableague = new ArrayList();
            for (int i = sort.Count - 1; i >= 0; i--)
            {

                foreach (DictionaryEntry entry in Otherleaguematchs)
                {
                    List<String> l = entry.Value as List<String>;
                    if (l.Count == Convert.ToInt32(sort[i]) && showotherleaguecount < OddsUntil.ShowOtherLeagueMatchCount)
                    {
                        showtableague.Add(entry.Key as String);
                        showotherleaguecount++;
                        foreach (String mId in l)
                        {
                            MatchItem mi = Uicontrol.getMatchData().getMatchItem(mId);
                            LeagueItem leagueitem = Uicontrol.getMatchData().getLeagueItem(entry.Key as String);
                            String lId = entry.Key as String;
                            ObservableCollection<MatchListDataTemple> matchlist = OddsUntil.LeagueViewTemple[mi.lId] as ObservableCollection<MatchListDataTemple>;

                            if (matchlist == null)
                            {
                                matchlist = new ObservableCollection<MatchListDataTemple>();

                                CompanyOdds sh = Uicontrol.getMatchData().companymatchsodds["1"] as CompanyOdds;
                                OddsInfo odds_asia = null, odds_eupro = null, odds_bigsmall = null;

                                if (sh != null)
                                {
                                    Hashtable mList = sh.o_data.MatchList[mi.mId] as Hashtable;

                                    OddsUntil.GetDataBymList(ref odds_asia, ref odds_eupro, ref odds_bigsmall, ref mList);

                                }

                                if (odds_asia != null && odds_eupro != null && odds_bigsmall != null)
                                {
                                    MatchListDataTemple matchlistdatatemple = new MatchListDataTemple
                                    {
                                        LeagueName = leagueitem.cnName,
                                        LeagueColor = leagueitem.color,
                                        MatchHost = mi.t1CnName,
                                        MatchVisit = mi.t2CnName,
                                        CompanyName = "澳门",
                                        CompanyColor = OddsUntil.White,
                                        Odds_a_host = odds_asia.host,
                                        Odds_a_vs = OddsUntil.GetVsName(odds_asia.vs, "tr"),
                                        Odds_a_visit = odds_asia.visit,

                                        Odds_e_host = odds_eupro.host,
                                        Odds_e_vs = odds_eupro.vs,
                                        Odds_e_visit = odds_eupro.visit,

                                        Odds_o_host = odds_bigsmall.host,
                                        Odds_o_vs = OddsUntil.GetTotalName(odds_bigsmall.vs),
                                        Odds_o_visit = odds_bigsmall.visit,
                                        mId = mi.mId,
                                        MatchKoTime = mi.koDay,

                                        Odds_a_host_color = OddsUntil.White,
                                        Odds_a_visit_color = OddsUntil.White,

                                        Odds_o_host_color = OddsUntil.White,
                                        Odds_o_visit_color = OddsUntil.White,

                                        Odds_e_host_color = OddsUntil.White,
                                        Odds_e_visit_color = OddsUntil.White,

                                        Odds_a_chgtime = odds_asia.chgTime,
                                        Odds_e_chgtime = odds_eupro.chgTime,
                                        Odds_o_chgtime = odds_bigsmall.chgTime,


                                    };


                                    matchlist.Add(matchlistdatatemple);

                                    TabItem tb = tb = new TabItem();
                                    tb.DataContext = lId;

                                    ListView listview = Uicontrol.NewListView();

                                    tb.Content = listview;
                                    tb.Header = leagueitem.cnName;

                                    listview.ItemsSource = matchlist;
                                    // 保存联盟赛事的显示信息模版
                                    OddsUntil.LeagueViewTemple[tb.DataContext as String] = matchlist;

                                    if (!Uicontrol.getLeagueTab().Items.Contains(tb))
                                    {
                                        Uicontrol.getLeagueTab().Items.Add(tb);
                                    }
                                
                                }
                               
                            }
                            else
                            {
                                CompanyOdds sh = this.Uicontrol.getMatchData().companymatchsodds["1"] as CompanyOdds;
                                OddsInfo odds_asia = null, odds_eupro = null, odds_bigsmall = null;

                                if (sh != null)
                                {
                                    Hashtable mList = sh.o_data.MatchList[mi.mId] as Hashtable;

                                    OddsUntil.GetDataBymList(ref odds_asia, ref odds_eupro, ref odds_bigsmall, ref mList);

                                }
                                MatchListDataTemple matchlistdatatemple = new MatchListDataTemple
                                {
                                    LeagueName = leagueitem.cnName,
                                    LeagueColor = leagueitem.color,
                                    MatchHost = mi.t1CnName,
                                    MatchVisit = mi.t2CnName,
                                    CompanyName = "澳门",
                                    CompanyColor = OddsUntil.White,
                                    Odds_a_host = odds_asia.host,
                                    Odds_a_vs = OddsUntil.GetVsName(odds_asia.vs, "tr"),
                                    Odds_a_visit = odds_asia.visit,

                                    Odds_e_host = odds_eupro.host,
                                    Odds_e_vs = odds_eupro.vs,
                                    Odds_e_visit = odds_eupro.visit,

                                    Odds_o_host = odds_bigsmall.host,
                                    Odds_o_vs = OddsUntil.GetTotalName(odds_bigsmall.vs),
                                    Odds_o_visit = odds_bigsmall.visit,
                                    mId = mi.mId,
                                    MatchKoTime = mi.koDay,

                                    Odds_a_host_color = OddsUntil.White,
                                    Odds_a_visit_color = OddsUntil.White,

                                    Odds_o_host_color = OddsUntil.White,
                                    Odds_o_visit_color = OddsUntil.White,

                                    Odds_e_host_color = OddsUntil.White,
                                    Odds_e_visit_color = OddsUntil.White,

                                    Odds_a_chgtime = odds_asia.chgTime,
                                    Odds_e_chgtime = odds_eupro.chgTime,
                                    Odds_o_chgtime = odds_bigsmall.chgTime,


                                };
                                // 保存比赛显示信息模版
                                OddsUntil.MathViewTemple[OddsUntil.GetMathViewKey(mi.mId, sh.cId)] = matchlistdatatemple;
                                if (!OddsUntil.IsInMatchListDataTemple(matchlist, matchlistdatatemple))
                                    matchlist.Add(matchlistdatatemple);
                            }

                        }


                    }
 
                }


           
            }


            TabItem other = this.Uicontrol.getTabItemByName("其他");
            ListView listviewother = null;
            if (other == null)
            {
                other = new TabItem();
                other.Header = "其他";
                other.Content = this.Uicontrol.NewListView(); ;
                this.Uicontrol.getLeagueTab().Items.Add(other);
                other.ContextMenu = new ContextMenu();
            }

            listviewother = other.Content as ListView;
            

            
            

            foreach (DictionaryEntry entry in Otherleaguematchs)
            {
                if (showtableague.Contains(entry.Key as String))
                    continue;

                LeagueItem leagueitem = Uicontrol.getMatchData().getLeagueItem(entry.Key as String);

                String lId = entry.Key as String;
                bool bfind = false;
                foreach (MenuItem m in other.ContextMenu.Items)
                {
                    if (m.Header as String  == leagueitem.cnName)
                        bfind = true;
                }
                if (!bfind)
                {
                    MenuItem item = new MenuItem();
                    item.Header = leagueitem.cnName;
                    item.Click += new RoutedEventHandler(item_Click);
                    other.ContextMenu.Items.Add(item);
                }
                

                ObservableCollection<MatchListDataTemple> matchlist = new ObservableCollection<MatchListDataTemple>();

                foreach (MatchItem matchitem in this.Uicontrol.getMatchData().Matchs.Values)
                {
                    if (matchitem.lId == lId)
                    {
                        
                        CompanyOdds sh = this.Uicontrol.getMatchData().companymatchsodds["1"] as CompanyOdds;
                        OddsInfo odds_asia = null, odds_eupro = null, odds_bigsmall = null;

                        if (sh != null)
                        {
                            Hashtable mList = sh.o_data.MatchList[matchitem.mId] as Hashtable;

                            OddsUntil.GetDataBymList(ref odds_asia, ref odds_eupro, ref odds_bigsmall, ref mList);

                        }

                        MatchListDataTemple matchlistdatatemple = new MatchListDataTemple
                        {
                            LeagueName = leagueitem.cnName,
                            LeagueColor = leagueitem.color,
                            MatchHost = matchitem.t1CnName,
                            MatchVisit = matchitem.t2CnName,
                            CompanyName = "澳门",
                            CompanyColor = OddsUntil.White,
                            Odds_a_host = odds_asia.host,
                            Odds_a_vs = OddsUntil.GetVsName(odds_asia.vs, "tr"),
                            Odds_a_visit = odds_asia.visit,

                            Odds_e_host = odds_eupro.host,
                            Odds_e_vs = odds_eupro.vs,
                            Odds_e_visit = odds_eupro.visit,

                            Odds_o_host = odds_bigsmall.host,
                            Odds_o_vs = OddsUntil.GetTotalName(odds_bigsmall.vs),
                            Odds_o_visit = odds_bigsmall.visit,
                            mId = matchitem.mId,
                            MatchKoTime = matchitem.koDay,

                            Odds_a_host_color = OddsUntil.White,
                            Odds_a_visit_color = OddsUntil.White,

                            Odds_o_host_color = OddsUntil.White,
                            Odds_o_visit_color = OddsUntil.White,

                            Odds_e_host_color = OddsUntil.White,
                            Odds_e_visit_color = OddsUntil.White,

                            Odds_a_chgtime = odds_asia.chgTime,
                            Odds_e_chgtime = odds_eupro.chgTime,
                            Odds_o_chgtime = odds_bigsmall.chgTime,
                        };

                        OddsUntil.MathViewTemple[OddsUntil.GetMathViewKey(matchitem.mId, sh.cId)] = matchlistdatatemple;
                        if (!OddsUntil.IsInMatchListDataTemple(matchlist, matchlistdatatemple))
                            matchlist.Add(matchlistdatatemple);

                    }
                }

                OddsUntil.LeagueViewTemple[lId] = matchlist;
            }

            //foreach (TabItem tb in this.Uicontrol.getLeagueTab().Items)
            //{
            //    ListView lv = tb.Content as ListView;

            //    if (lv != null)
            //    {
            //        foreach (ListViewItem li in lv.)
            //        {
            //            li.ToolTip = "双击打开查看具体场次信息";
            //        }
            //    }
            //}
            
        }
        // 监听遍历比赛场次
        public void OnListenMathItem(MatchItem ci, MainUIControl ui)
        {
            LeagueItem leagueitem = ui.getMatchData().LeagueList[ci.lId] as LeagueItem;

            // 判断是否默认联盟的
            if (leagueitem != null)
            {
                // 属于默认联盟的
                if (OddsUntil.defaultLeague.Contains(leagueitem.cnName))
                {
                    ObservableCollection<MatchListDataTemple> matchlist = OddsUntil.LeagueViewTemple[ci.lId as String] as ObservableCollection<MatchListDataTemple>;

                    if (matchlist == null)
                    {
                        matchlist = new ObservableCollection<MatchListDataTemple>();

                        CompanyOdds sh = ui.getMatchData().companymatchsodds["1"] as CompanyOdds;
                        OddsInfo odds_asia = null, odds_eupro = null, odds_bigsmall = null;

                        if (sh != null)
                        {
                            Hashtable mList = sh.o_data.MatchList[ci.mId] as Hashtable;

                            OddsUntil.GetDataBymList(ref odds_asia, ref odds_eupro, ref odds_bigsmall, ref mList);

                        }
                        if (odds_asia != null && odds_eupro != null && odds_bigsmall != null)
                        {

                            MatchListDataTemple matchlistdatatemple = new MatchListDataTemple
                            {
                                LeagueName = leagueitem.cnName,
                                LeagueColor = leagueitem.color,
                                MatchHost = ci.t1CnName,
                                MatchVisit = ci.t2CnName,
                                CompanyName = "澳门",
                                CompanyColor = OddsUntil.White,
                                Odds_a_host = odds_asia.host,
                                Odds_a_vs = OddsUntil.GetVsName(odds_asia.vs, "tr"),
                                Odds_a_visit = odds_asia.visit,

                                Odds_e_host = odds_eupro.host,
                                Odds_e_vs = odds_eupro.vs,
                                Odds_e_visit = odds_eupro.visit,

                                Odds_o_host = odds_bigsmall.host,
                                Odds_o_vs = OddsUntil.GetTotalName(odds_bigsmall.vs),
                                Odds_o_visit = odds_bigsmall.visit,
                                mId = ci.mId,
                                MatchKoTime = ci.koDay,

                                Odds_a_host_color = OddsUntil.White,
                                Odds_a_visit_color = OddsUntil.White,

                                Odds_o_host_color = OddsUntil.White,
                                Odds_o_visit_color = OddsUntil.White,

                                Odds_e_host_color = OddsUntil.White,
                                Odds_e_visit_color = OddsUntil.White,

                                Odds_a_chgtime = odds_asia.chgTime,
                                Odds_e_chgtime = odds_eupro.chgTime,
                                Odds_o_chgtime = odds_bigsmall.chgTime,


                            };


                            matchlist.Add(matchlistdatatemple);

                            TabItem tb = tb = new TabItem();


                            ListView listview = Uicontrol.NewListView();

                            tb.Content = listview;
                            tb.DataContext = ci.lId;
                            tb.Header = leagueitem.cnName;

                            listview.ItemsSource = matchlist;

                            OddsUntil.LeagueViewTemple.Add(ci.lId, matchlist);

                            if (!Uicontrol.getLeagueTab().Items.Contains(tb))
                            {
                                Uicontrol.getLeagueTab().Items.Add(tb);
                            }
                        
                        }
                        


                    }
                    else
                    {
                        CompanyOdds sh = ui.getMatchData().companymatchsodds["1"] as CompanyOdds;
                        OddsInfo odds_asia = null, odds_eupro = null, odds_bigsmall = null;

                        if (sh != null)
                        {
                            Hashtable mList = sh.o_data.MatchList[ci.mId] as Hashtable;

                            OddsUntil.GetDataBymList(ref odds_asia, ref odds_eupro, ref odds_bigsmall, ref mList);

                        }
                        MatchListDataTemple matchlistdatatemple = new MatchListDataTemple
                        {
                            LeagueName = leagueitem.cnName,
                            LeagueColor = leagueitem.color,
                            MatchHost = ci.t1CnName,
                            MatchVisit = ci.t2CnName,
                            CompanyName = "澳门",
                            CompanyColor = OddsUntil.White,
                            Odds_a_host = odds_asia.host,
                            Odds_a_vs = OddsUntil.GetVsName(odds_asia.vs, "tr"),
                            Odds_a_visit = odds_asia.visit,

                            Odds_e_host = odds_eupro.host,
                            Odds_e_vs = odds_eupro.vs,
                            Odds_e_visit = odds_eupro.visit,

                            Odds_o_host = odds_bigsmall.host,
                            Odds_o_vs = OddsUntil.GetTotalName(odds_bigsmall.vs),
                            Odds_o_visit = odds_bigsmall.visit,
                            mId = ci.mId,
                            MatchKoTime = ci.koDay,

                            Odds_a_host_color = OddsUntil.White,
                            Odds_a_visit_color = OddsUntil.White,

                            Odds_o_host_color = OddsUntil.White,
                            Odds_o_visit_color = OddsUntil.White,

                            Odds_e_host_color = OddsUntil.White,
                            Odds_e_visit_color = OddsUntil.White,

                            Odds_a_chgtime = odds_asia.chgTime,
                            Odds_e_chgtime = odds_eupro.chgTime,
                            Odds_o_chgtime = odds_bigsmall.chgTime,


                        };
                        // 保存比赛显示信息模版
                        OddsUntil.MathViewTemple[OddsUntil.GetMathViewKey(ci.mId, sh.cId)] = matchlistdatatemple;

                        if(!OddsUntil.IsInMatchListDataTemple(matchlist,matchlistdatatemple))
                            matchlist.Add(matchlistdatatemple);
                       

                    }
                }
                else//非默认联赛
                {
                    List<String> mIdsList = Otherleaguematchs[ci.lId] as List<String>;


                    if (mIdsList == null)
                    {
                        mIdsList = new List<String>();
                        Otherleaguematchs.Add(ci.lId, mIdsList);
                    }
                    mIdsList.Add(ci.mId);
                    
                }
            }
          
        }
    }
    public class MatchListDataTempleComparer : IComparer<MatchListDataTemple>
    {
        public int Compare(MatchListDataTemple x,MatchListDataTemple y)
        {
            if (DateTime.Parse(x.MatchKoTime).Ticks - DateTime.Parse(y.MatchKoTime).Ticks > 0)
                return 0;
            else
                return 1;
        }
    }
}
